<?php
/**
 * @package YARBB
 * @copyright Авторские права (C) 2009 www.yar-it.com. Все права защищены.
 * @license Лицензия http://www.gnu.org/licenses/gpl-2.0.htm GNU/GPL, или help/license.php
 * YARBB! - свободное программное обеспечение распространяемое по условиям лицензии GNU/GPL
 * Для получения информации о используемых расширениях и замечаний об авторском праве, смотрите файл copyright.php.
 */
// don't allow direct linking
defined( '_VALID_MOS' ) or die();

//запрет чтения форума группе
if ($yarbb_user['g_read_board'] == '0') {
	mosRedirect(sefRelToAbs(YARBB_URL.'&amp;task=access&amp;action=noperm'));
}elseif ($yarbb_user['g_search'] == '0') {
	mosRedirect(sefRelToAbs(YARBB_URL), _Y_YARNONSEARCH);
}

// Detect two byte character sets

/* boston ТУТ кажется не нужно */
//$lang_multibyte = true;
//$multibyte = (isset($lang_multibyte) && $lang_multibyte) ? true : false;

// Figure out what to do :-)
if (isset($_GET['action']) || isset($_GET['search_id'])) {
	/*****************************************************/
	/* PERFORM SEARCH */
	/*****************************************************/
	$action = (isset($_GET['action'])) ? $_GET['action'] : null;
	$forum = (isset($_GET['forum'])) ? intval($_GET['forum']) : -1;
	$sort_dir = (isset($_GET['sort_dir'])) ? (($_GET['sort_dir'] == 'DESC') ? 'DESC' : 'ASC') : 'DESC';
	if ($action == 'search') {
		$keywords = (isset($_GET['keywords'])) ? strtolower(trim($_GET['keywords'])) : null;
		$author = (isset($_GET['author'])) ? strtolower(trim($_GET['author'])) : null;
		if (preg_match('#^[\*%]+$#', $keywords) || strlen(str_replace(array('*', '%'), '', $keywords)) < 1)
			$keywords = '';
		if (preg_match('#^[\*%]+$#', $author) || strlen(str_replace(array('*', '%'), '', $author)) < 1)
			$author = '';
		if (!$keywords && !$author)
			mosRedirect(sefRelToAbs(YARBB_URL.'&amp;task=search'), _Y_YARNOTERM);
		if ($author)
			$author = str_replace('*', '%', $author);
		$show_as = (isset($_GET['show_as'])) ? $_GET['show_as'] : 'posts';
		$sort_by = (isset($_GET['sort_by'])) ? intval($_GET['sort_by']) : null;
		$search_in = (!isset($_GET['search_in']) || $_GET['search_in'] == 'all') ? 0 : (($_GET['search_in'] == 'message') ? 1 : -1);
	}
	// If it's a user search (by id)
	else if ($action == 'show_user') {
		$user_id = intval($_GET['user_id']);
		if ($user_id < 2) {
			//exit("2");
			mosRedirect(sefRelToAbs(YARBB_URL.'&amp;task=search'), _Y_404);
		}
	}
	// If it's a label search
	else if ($action == 'show_label') {
		if (!isset($_GET['text'])) {
			//exit("3");
			mosRedirect(sefRelToAbs(YARBB_URL.'&amp;task=search'), _Y_404);
		}
		$label = strtolower(trim($_GET['text']));
	}
	else {
		if ($action != 'show_new' && $action != 'show_24h' && $action != 'show_unanswered' && $action != 'show_subscriptions') {
			//exit("4");
			mosRedirect(sefRelToAbs(YARBB_URL.'&amp;task=search'), _Y_404);
		}
	}
	// Search a specific forum?
	$forum_sql = ($forum != -1) ? ' AND t.forum_id = '.$forum : '';
	if (!empty($author) || !empty($keywords)) {
		$group_by_sql = '';
		switch ($sort_by) {
			case 1:
				$sort_by_sql = ($show_as == 'topics') ? 't.poster' : 'p.poster';
				break;
			case 2:
				$sort_by_sql = 't.subject';
				break;
			case 3:
				$sort_by_sql = 't.forum_id';
				break;
			default: {
					$sort_by_sql = ($show_as == 'topics') ? 't.posted' : 'p.posted';
					if ($show_as == 'topics')
						$group_by_sql = ', t.posted';
					break;
				}
		}
		if(!empty($author) && !empty($keywords))
			$author_sql = ' AND p.poster = \''.$database->getEscaped($author).'\'';
		else
			$author_sql = '';
		$select_sql = '';
		$group_by_sql = '';
		if ($show_as == 'posts') {
			if(!empty($keywords)) {
				// Поиск ключевого слова
				$match_msg = 'MATCH (p.message) AGAINST (\''.$database->getEscaped($keywords).'\')';
				if($sort_by == 5)
					$sort_by_sql = 'score';
				$select_sql = $match_msg.' AS score, ';
				$match_sql = $match_msg;
			}
			else {
				// Поиск автора
				$match_sql = 'p.poster = \''.$database->getEscaped($author).'\'';
				if($sort_by == 5)
					$sort_by_sql = 'p.posted';
			}
			$substr_sql = ($db_type != 'sqlite') ? 'SUBSTRING' : 'SUBSTR';
			$select_sql .= 'p.id AS pid, p.poster AS pposter, p.posted AS pposted, p.poster_id, '.$substr_sql.'(p.message, 1, 1000) AS message, t.id AS tid, t.poster, t.subject, t.last_post, t.last_post_id, t.last_poster, t.num_replies, t.forum_id';
		}
		else {
			if(!empty($keywords)) {
				// Поиск ключевого слова
				$match_msg = 'MATCH (p.message) AGAINST (\''.$database->getEscaped($keywords).'\')';
				$match_subject = 'MATCH (t.subject) AGAINST (\''.$database->getEscaped($keywords).'\')';
				if($sort_by == 5)
					$sort_by_sql = 'score';
				$select_sql = $match_msg.' + '.$match_subject.' AS score, ';
				$match_sql = '('.$match_msg.' OR '.$match_subject.')';
			}
			else {
				// Поиск автора
				$match_sql = 'p.poster = \''.$database->getEscaped($author).'\'';
				if($sort_by == 5)
					$sort_by_sql = 't.posted';
			}

			$select_sql .= 't.id AS tid, t.poster, t.subject, t.last_post, t.last_post_id, t.last_poster, t.num_replies, t.closed, t.forum_id, t.labels';

			$group_by_sql = ' GROUP BY t.id, t.poster, t.subject, t.last_post, t.last_post_id, t.last_poster, t.num_replies, t.closed, t.forum_id'.$group_by_sql;
		}
		//$sql = 'SELECT '.$select_sql.'
		//FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON p.topic_id = t.id INNER JOIN '.$db->prefix.'forums AS f ON t.forum_id = f.id
		//WHERE '.$match_sql.$forum_sql.$author_sql.$group_by_sql.' ORDER BY '.$sort_by_sql;
		$perm_sql='(fp.read_forum IS NULL OR fp.read_forum=1) AND ';
//s
		$sql = 'SELECT '.$select_sql.'
            		FROM #__forum_posts AS p INNER JOIN 
					#__forum_topics AS t ON p.topic_id = t.id INNER JOIN 
					#__forum_forums AS f ON t.forum_id = f.id LEFT JOIN 
					#__forum_forum_perms AS fp ON(fp.forum_id=f.id AND fp.group_id='.$yarbb_user['g_id'].')
           			WHERE '.$perm_sql.$match_sql.$forum_sql.$author_sql.$group_by_sql.' 
					ORDER BY '.$sort_by_sql;
		// Get the number of hits
		$num_hits = $database->setQuery($sql)->loadResult();
		// Exit if no hits
		if($num_hits == 0)
			mosRedirect(sefRelToAbs(YARBB_URL.'&amp;task=search'), _Y_NOKEYWORD);
	}
	else if ($action == 'show_new' || $action == 'show_24h' || $action == 'show_user' ||  $action == 'show_label' || $action == 'show_subscriptions' || $action == 'show_unanswered') {
		$sort_by_sql = ' ORDER BY t.last_post';
		$show_as = 'topics';
		$select_sql = 't.id AS tid, t.poster, t.subject, t.last_post, t.last_post_id, t.last_poster, t.num_replies, t.closed, t.forum_id, t.labels';
		// If it's a search for new posts
		if ($action == 'show_new') {
			if ($yarbb_user['is_guest'])
				mosRedirect(sefRelToAbs(YARBB_URL), _NOT_AUTH);
			//s
			$sql = 'SELECT '.$select_sql.' FROM
					#__forum_topics AS t INNER JOIN 
					#__forum_forums AS f ON f.id=t.forum_id LEFT JOIN 
					#__forum_forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$yarbb_user['g_id'].') 
					WHERE (fp.read_forum IS NULL OR fp.read_forum=1) 
					AND t.last_post>'.$yarbb_user['last_visit'].$sort_by_sql;

			$num_hits = $database->setQuery($sql)->loadResult();
			if (!$num_hits)
				mosRedirect(sefRelToAbs(YARBB_URL), _Y_YARNOPOSTSEARN);
		}
		// If it's a search for todays posts
		else if ($action == 'show_24h') {
			//s
			$sql = 'SELECT '.$select_sql.' FROM
					#__forum_topics AS t INNER JOIN 
					#__forum_forums AS f ON f.id=t.forum_id LEFT JOIN 
					#__forum_forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$yarbb_user['g_id'].') 
					WHERE (fp.read_forum IS NULL OR fp.read_forum=1) 
					AND t.last_post>'.(time() - 86400).$sort_by_sql;

			$num_hits = $database->setQuery($sql)->loadResult();
			if (!$num_hits)
				mosRedirect(sefRelToAbs(YARBB_URL), _Y_YAR24HOUR);
		}
		// If it's a search for posts by a specific user ID
		else if ($action == 'show_user') {
			$sql = 'SELECT '.$select_sql.' FROM
				#__forum_topics AS t INNER JOIN 
				#__forum_posts AS p ON t.id=p.topic_id INNER JOIN 
				#__forum_forums AS f ON f.id=t.forum_id LEFT JOIN 
				#__forum_forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$yarbb_user['g_id'].') 
				WHERE (fp.read_forum IS NULL OR fp.read_forum=1) 
				AND p.poster_id='.$user_id.' GROUP BY t.id'.$sort_by_sql;

			$num_hits = $database->setQuery($sql)->loadResult();
			if (!$num_hits)
				mosRedirect(sefRelToAbs(YARBB_URL), _Y_NOKEYWORD);
		}

		// If it's a search for topics marked by some label
		else if ($action == 'show_label') {
			if (empty($yarbb_config['o_topic_labels']))
				mosRedirect(sefRelToAbs(YARBB_URL), 'нет меток1');

			$cond = $database->getEscaped($label);
			$sql = 'SELECT t.id FROM
						#__forum_topics AS t INNER JOIN 
						#__forum_forums AS f ON f.id=t.forum_id LEFT JOIN 
						#__forum_forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$yarbb_user['g_id'].') 
						WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.labels like \'%,'.$cond.',%\'';
			$num_hits = $database->setQuery($sql)->loadResultArray();

			/*if (!$num_hits)
					mosRedirect(sefRelToAbs(YARBB_URL), 'нет меток2'.$num_hits);*/
		}

		// If it's a search for subscribed topics
		else if ($action == 'show_subscriptions') {
			if ($yarbb_user['is_guest']) {
				//exit("5");
				mosRedirect(sefRelToAbs(YARBB_URL), _Y_404);
			}
			$sql = 'SELECT '.$select_sql.' FROM #__forum_topics AS t INNER JOIN
				#__forum_subscriptions AS s 
				ON (t.id=s.topic_id AND s.user_id='.$yarbb_user['id'].') INNER JOIN 
				#__forum_forums AS f ON f.id=t.forum_id LEFT JOIN 
				#__forum_forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$yarbb_user['g_id'].') 
				WHERE (fp.read_forum IS NULL OR fp.read_forum=1)'.$sort_by_sql;

			$num_hits = $database->setQuery($sql)->loadResult();
			if (!$num_hits)
				mosRedirect(sefRelToAbs(YARBB_URL), _Y_YASEARCHNOSUB);
		}
		// If it's a search for unanswered posts
		else {
			$sql = 'SELECT '.$select_sql.' FROM
					#__forum_topics AS t INNER JOIN 
					#__forum_forums AS f ON f.id=t.forum_id LEFT JOIN 
					#__forum_forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$yarbb_user['g_id'].')
					 WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.num_replies=0 AND t.moved_to IS NULL'.$sort_by_sql;
			$num_hits = $database->setQuery($sql)->loadResult();
			if (!$num_hits)
				mosRedirect(sefRelToAbs(YARBB_URL), _Y_YARNOREPPOSTSE);
		}
	}
	else {
		//exit("1");
		mosRedirect(sefRelToAbs(YARBB_URL), _Y_404);
	}
	if ($num_hits) {
		// Determine the topic or post offset (based on $_GET['page'])
		$per_page = ($show_as == 'posts') ? $yarbb_user['disp_posts'] : $yarbb_user['disp_topics'];
		$num_pages = ceil($num_hits+1 / $per_page);
		$p = (!isset($_GET['page']) || $_GET['page'] <= 1 || $_GET['page'] > $num_pages) ? 1 : $_GET['page'];
		$start_from = $per_page * ($p - 1);
		// Generate paging links
		$page_url = array();
		foreach($_GET as $key => $value) {
			if($key == 'page')
				continue;
			$page_url[] = $key.'='.$value;
		}
		$paging_links = _PN_PAGE.': '.paginate($num_pages, $p, YARBB_URL.'&amp;task=search&amp;'.implode('&', $page_url));
		$sql .= ' '.$sort_dir.' LIMIT '.$start_from.', '.$per_page;
		//$result = $database->setQuery($sql);
		$search_set = array();
		//s
		$rows=$database->setQuery($sql)->loadAssocList();
		foreach  ($rows as $row )
			$search_set[] = $row;
		//	$db->free_result($result);
		/*****************************************************/
		/* DISPLAY HITS */
		/*****************************************************/
		echo $mainframe->setPageTitle( _Y_YARSEARCHRES );

		?>
<div class="linkst">
	<div class="inbox">
		<p class="pagelink"><?php echo $paging_links ?></p>
	</div>
</div>

		<?php

		//Set background switching on for show as posts
		$bg_switch = true;

		if ($show_as == 'topics') {

			?>
<div id="vf" class="blocktable">
	<h2><span><?php echo _Y_YARSEARCHRES; ?></span></h2>
	<div class="box">
		<div class="inbox">
			<table cellspacing="0">
				<thead>
					<tr>
						<th class="tcl" scope="col"><?php echo _Y_TOPICS; ?></th>
						<th class="ag_tcr" scope="col"><?php echo _Y_REPLYSY; ?></th>
					</tr>
				</thead>
				<tbody>
			<?php
		}
		// Fetch the list of forums
		//s
		$sql='SELECT id, forum_name FROM #__forum_forums';
		$forum_list = array();
		$forum_list=$database->setQuery($sql)->loadRowList();

		// Finally, lets loop through the results and output them
		for ($i = 0; $i < count($search_set); ++$i) {
			@reset($forum_list);
			while (list(, $temp) = @each($forum_list)) {
				if ($temp[0] == $search_set[$i]['forum_id'])
					$forum = '<a href="'.sefRelToAbs(YARBB_URL.'&amp;task=viewforum&amp;id='.$temp[0]).'">'.yarbb_htmlspecialchars($temp[1]).'</a>';
			}

			if ($yarbb_config['o_censoring'] == '1')
				$search_set[$i]['subject'] = censor_words($search_set[$i]['subject']);

			if ($show_as == 'posts') {

				if ($search_set[$i]['question'] == "")
					$subject = '<a href="'.sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;id='.$search_set[$i]['tid']).'">'.yarbb_htmlspecialchars($search_set[$i]['subject']).'</a>';
				else
					$subject = _Y_YARPOLLFRONT . ': <a href="'.sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;id='.$search_set[$i]['tid']).'">'.yarbb_htmlspecialchars($search_set[$i]['subject']).'</a>';

				if (!$yarbb_user['is_guest'] && $search_set[$i]['last_post'] > $yarbb_user['last_visit'])
					if ($yarbb_config['o_censoring'] == '1')
						$search_set[$i]['message'] = censor_words($search_set[$i]['message']);
						$message = str_replace("\n", '<br />', yarbb_htmlspecialchars($search_set[$i]['message']));
						$pposter = yarbb_htmlspecialchars($search_set[$i]['pposter']);

					if ($search_set[$i]['poster_id'] > 1)
						$pposter = '<strong><a href="'.sefRelToAbs('index.php?option=com_users&task=profile&amp;user='.$search_set[$i]['poster_id']).'">'.$pposter.'</a></strong>';

					if (yarbb_strlen($message) >= 1000)
						$message .= ' &hellip;';

					$vtpost1 = ($i == 0) ? ' vtp1' : '';

					// Switch the background color for every message.
					$bg_switch = ($bg_switch) ? $bg_switch = false : $bg_switch = true;
					$vtbg = ($bg_switch) ? ' rowodd' : ' roweven';
		?>
				<div class="blockpost searchposts<?php echo $vtbg ?>">
					<h2><?php echo $forum ?>&nbsp;&raquo;&nbsp;<?php echo $subject ?>&nbsp;&raquo;&nbsp;<a href="<?php echo sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;pid='.$search_set[$i]['pid']).'#p'.$search_set[$i]['pid'] ?>"><?php echo format_time($search_set[$i]['pposted']) ?></a></h2>
					<div class="box">
						<div class="inbox">
							<div class="postleft">
								<dl>
									<dt><?php echo $pposter ?></dt>
									<dd><?php echo _Y_REPLYSY; ?>: <?php echo $search_set[$i]['num_replies'] ?></dd>
									<dd><?php echo $icon; ?></dd>
									<dd><p class="clearb"><a href="<?php echo sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;pid='.$search_set[$i]['pid']).'#p'.$search_set[$i]['pid'] ?>"><?php echo _Y_YARGOTO; ?></a></p></dd>
								</dl>
							</div>
							<div class="postright">
								<div class="postmsg">
									<p><?php echo $message ?></p>
								</div>
							</div>
							<div class="clearer"></div>
						</div>
					</div>
				</div>
		<?php
			} else {
				$icon = '<div class="icon"><div class="nosize">'._Y_NORMALICON.'</div></div>'."\n";
				$item_status = '';
				$icon_type = 'icon';
				if ($search_set[$i]['question'] == "")
					$subject = '<a href="'.sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;id='.$search_set[$i]['tid']).'">'.yarbb_htmlspecialchars($search_set[$i]['subject']).'</a> <span class="byuser">'._Y_YARBY.'&nbsp;'.yarbb_htmlspecialchars($search_set[$i]['poster']).'</span>';
				else
					$subject = _Y_YARPOLLFRONT . ': <a href="'.sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;id='.$search_set[$i]['tid']).'">'.yarbb_htmlspecialchars($search_set[$i]['subject']).'</a> <span class="byuser">'._Y_YARBY.'&nbsp;'.yarbb_htmlspecialchars($search_set[$i]['poster']).'</span> [ '.yarbb_htmlspecialchars($search_set[$i]['question']).' ]';
				if ($search_set[$i]['closed'] != '0') {
					$item_status = 'iclosed';
				}
				if (!$yarbb_user['is_guest'] && $search_set[$i]['last_post'] > $yarbb_user['last_visit']) {
					$item_status .= ' inew';
					$icon_type = 'icon inew';
					$subject = '<strong>'.$subject.'</strong>';
					$subject_new_posts = '<span class="newtext">[&nbsp;<a href="'.sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;id='.$search_set[$i]['tid'].'&amp;action=new').'" title="'._Y_YAGOTONEWTOP.'">
					<img src="'.$yarbb_live.'/style/images/'.$yarbb_config['o_default_style'].'/folder/folder_new_hot.gif" title="'._Y_YARNEWPOSTS22.'" alt="hot" /></a>&nbsp;]</span>';
				} else
					$subject_new_posts = null;
				$num_pages_topic = ceil(($search_set[$i]['num_replies'] + 1) / $yarbb_user['disp_posts']);
				if ($num_pages_topic > 1)
					$subject_multipage = '[ '.paginate($num_pages_topic, -1, YARBB_URL.'&amp;task=viewtopic&amp;id='.$search_set[$i]['tid']).' ]';
				else
					$subject_multipage = null;
				// Should we show the "New posts" and/or the multipage links?
				if (!empty($subject_new_posts) || !empty($subject_multipage)) {
					$subject .= '&nbsp; '.(!empty($subject_new_posts) ? $subject_new_posts : '');
					$subject .= !empty($subject_multipage) ? ' '.$subject_multipage : '';
								}
					$labels = (!empty($yarbb_config['o_topic_labels']))? $search_set[$i]['labels']: '';
								?>
				<tr<?php if ($item_status != '') echo ' class="'.trim($item_status).'"'; ?>>
					<td class="tcl">
						<div class="intd">
							<div class="<?php echo $icon_type ?>"><div class="nosize"><?php echo trim($icon_text) ?></div></div>
							<div class="tclcon">
								<?php echo $subject."\n" ?> <br />&nbsp; &nbsp;<strong><i><?php echo _Y_RSSFORUM; ?></i></strong>  <?php echo $forum ?> &nbsp; &nbsp; &nbsp;<strong><i><?php echo _Y_REPLYSY; ?>:</i></strong> <?php echo $search_set[$i]['num_replies'] ?>							</div>
								<?php if (!empty($labels)) echo '<div class="topiclabels">' . $lang_common['Labels'] . ': ['. show_labels($labels) . ']</div>'; ?>
						</div>
					</td>
								<?php
								if ($search_set[$i]['question'] == "") {
									?><td class="ag_tcr"><?php echo '<a href="'.sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;pid='.$search_set[$i]['last_post_id']).'#p'.$search_set[$i]['last_post_id'].'">'.format_time($search_set[$i]['last_post']).'</a> '._Y_YARBY.'&nbsp;'.yarbb_htmlspecialchars($search_set[$i]['last_poster']) ?></td><?php
								}
								else {
									?><td class="ag_tcr"><?php echo '<a href="'.sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;pid='.$search_set[$i]['last_post_id']).'#p'.$search_set[$i]['last_post_id'].'">'.format_time($search_set[$i]['last_post']).'</a> '._Y_YARBY.'&nbsp;'.yarbb_htmlspecialchars($search_set[$i]['last_poster']) ?></td><?php
								} ?>
				</tr>
								<?php

							}
						}
						if ($show_as == 'topics')
							echo "\t\t\t".'</tbody>'."\n\t\t\t".'</table>'."\n\t\t".'</div>'."\n\t".'</div>'."\n".'</div>'."\n\n";

						?>
				<div class="<?php echo ($show_as == 'topics') ? 'linksb' : 'postlinksb'; ?>">
					<div class="inbox">
						<p class="pagelink"><?php echo $paging_links ?></p>
					</div>
				</div>
						<?php

						$footer_style = 'search';
						require $yarbb_path.'/task/footer.php';
					}
				} else {
	echo $mainframe->setPageTitle( _Y_SEARCH_TITLE.' - '.yarbb_htmlspecialchars($yarbb_config['o_board_title']) );
	$focus_element = array('search', 'keywords');
	?>
				<div id="searchform" class="blockform">
					<h2><span><?php echo _Y_SEARCH_TITLE; ?></span></h2>
					<div class="box">
						<form id="yar_search" method="get" action="<?php echo sefRelToAbs(YARBB_URL.'&amp;task=search')?>">
							<div class="inform">
								<fieldset>
									<legend><?php echo _Y_YARCRITERSEAR; ?></legend>
									<div class="infldset">
					<input type="hidden" name="option" value="com_forum" />
					<input type="hidden" name="task" value="search" />
					<input type="hidden" name="action" value="search" />										
										<label class="conl"><?php echo _Y_PROMPT_KEYWORD; ?><br /><input type="text" name="keywords" size="40" maxlength="100" value="<?php echo yarbb_htmlspecialchars($_GET['keywords']);?>"/><br /></label>
										<label class="conl"><?php echo _Y_YARBY; ?><br /><input id="author" type="text" name="author" size="25" maxlength="25" /><br /></label>
										<p class="clearb"><?php echo _Y_YARSEARCHINFO; ?></p>
									</div>
								</fieldset>
							</div>
							<div class="inform">
								<fieldset>
									<legend><?php echo _Y_YARWHOISWHO; ?></legend>
									<div class="infldset">
										<label class="conl"><?php echo _Y_RSSFORUM; ?>
											<br /><select id="forum" name="forum">
					<?php
					if ($yarbb_config['o_search_all_forums'] == '1' || $yarbb_user['g_id'] < YARBB_GUEST || $yarbb_user['g_global_moderation'])
		echo "\t\t\t\t\t\t\t".'<option value="-1">'._Y_YARALLFOR.'</option>'."\n";
//s
	$sql = 'SELECT c.id AS cid, c.cat_name, f.id AS fid, f.forum_name, f.redirect_url, f.parent_forum_id FROM
		#__forum_categories AS c INNER JOIN 
		#__forum_forums AS f ON c.id=f.cat_id LEFT JOIN 
		#__forum_forum_perms AS fp ON (fp.forum_id=f.id 
		AND fp.group_id='.$yarbb_user['g_id'].') 
		WHERE (fp.read_forum IS NULL OR fp.read_forum=1) 
		AND f.redirect_url IS NULL ORDER BY c.disp_position, c.id, f.disp_position';

					$cur_category = 0;
					$rows=$database->setQuery($sql)->loadAssocList();

					foreach ($rows as $cur_forum ) {
		if ($cur_forum['cid'] != $cur_category)	// A new category since last iteration?
		{
			if ($cur_category)
				echo "\t\t\t\t\t\t\t".'</optgroup>'."\n";
			echo "\t\t\t\t\t\t\t".'<optgroup label="'.yarbb_htmlspecialchars($cur_forum['cat_name']).'">'."\n";
			$cur_category = $cur_forum['cid'];
		}
		if ($cur_forum['parent_forum_id'] == 0) {
			echo "\t\t\t\t\t\t\t\t".'<option
value="'.$cur_forum['fid'].'">'.yarbb_htmlspecialchars($cur_forum['forum_name']).'</option>'."\n";
		}
		else {
			echo "\t\t\t\t\t\t\t\t".'<option
value="'.$cur_forum['fid'].'">&nbsp;&nbsp;&nbsp;'.yarbb_htmlspecialchars($cur_forum['forum_name']).'</option>'."\n";
		}
	}
	?>
												</optgroup>
											</select>
											<br /></label>
										<p class="clearb"><?php echo _Y_YARSEARCHINFO2; ?></p>
									</div>
								</fieldset>
							</div>
							<div class="inform">
								<fieldset>
									<legend><?php echo _Y_YASHOWRES0; ?></legend>
									<div class="infldset">
										<label class="conl"><?php echo _Y_SORT_BY; ?>:
											<br /><select name="sort_by">
												<option value="5"><?php echo _Y_YARRELEVA; ?></option>
												<option value="0"><?php echo _Y_YASORTTIME; ?></option>
												<option value="1"><?php echo _Y_YASORTAVT; ?></option>
												<option value="2"><?php echo _Y_YASORTSUBG; ?></option>
												<option value="3"><?php echo _Y_YASORTFORUMS; ?></option>
											</select>
											<br /></label>
										<label class="conl"><?php echo _Y_YASORTORDER; ?>:
											<br /><select name="sort_dir">
												<option value="DESC"><?php echo _Y_YASORTORDER1; ?></option>
												<option value="ASC"><?php echo _Y_YASORTORDER2; ?></option>
											</select>
											<br /></label>
										<label class="conl"><?php echo _Y_YASHOWRES; ?>:
											<br /><select name="show_as">
												<option value="topics"><?php echo _Y_TOPICS; ?></option>
												<option value="posts"><?php echo _Y_PERPOST; ?></option>
											</select>
											<br /></label>
										<p class="clearb"><?php echo _Y_YASHOWRES2; ?></p>
									</div>
								</fieldset>
							</div>
							<p><input type="submit" value="<?php echo _Y_SEARCH_BOX; ?>" accesskey="s" /></p>
						</form>
					</div>
				</div>

	<?php
	require $yarbb_path.'/task/footer.php';
}
?>
